/*
 * Created on May 22, 2005
 *
 */
package org.mushroomdb.catalog.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.Register;


/**
 * @author Matias
 *
 */
public class FillableRegister implements Register {
	private List columns;
	private Map values;

	/**
	 * Constructor default.
	 */
	public FillableRegister() {
		this.columns = new LinkedList();
		this.values = new HashMap();
	}
	
	/**
	 * Agrega una columna a este registro.
	 * @param name
	 */
	public void addColumn(Column column) {
		this.columns.add(column);
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnNames()
	 */
	public Iterator getColumns() {
		return this.columns.iterator();
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnIndex(java.lang.String)
	 */
	public int getColumnIndex(Column column) {
		Iterator columns = this.getColumns();
		int index = 1;
		while (columns.hasNext()) {
			Column registerColumn = (Column) columns.next();
			if (registerColumn.equals(column)) {
				return index;
			}
			index++;
		}
		return -1;
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getValue(int)
	 */
	public Object getValue(int columnIndex) {
		return this.values.get(this.columns.get(columnIndex - 1));
	}

	/**
	 * @see org.mushroomdb.catalog.Register#setValue(int, java.lang.Object)
	 */
	public void setValue(int columnIndex, Object value) {
		this.values.put(this.columns.get(columnIndex - 1),value);
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnCount()
	 */
	public int getColumnCount() {
		return this.columns.size();
	}

	/**
	 * toString
	 */
	public String toString() {
		int columnCount = this.getColumnCount();
		String rv = ""; //$NON-NLS-1$
		
		for (int i = 1; i <= columnCount; i++) {
			if (rv.length() > 0) {
				rv = rv + ',';
			}
			
			Object object = this.getValue(i).toString();
			if (object instanceof String) {
				rv = rv + '\'' + object.toString() + '\'';
			} else {
				rv = rv + object.toString();
			}
		}
		
		return '(' + rv + ')';
	}

	/**
	 * @see org.mushroomdb.catalog.Register#remove()
	 */
	public void remove() {
		throw new UnsupportedOperationException("This register cannot be removed."); //$NON-NLS-1$
	}
}
